package cn.tanghom.content.controller.web;

import cn.tanghom.content.model.Article;
import cn.tanghom.content.model.ArticleCategory;
import cn.tanghom.content.service.ArticleCategoryService;
import cn.tanghom.content.service.ArticleService;
import cn.tanghom.conf.CommonKey;
import cn.tanghom.support.hook.ArticleModule;
import cn.tanghom.support.utils.CommonUtils;
import cn.tanghom.support.web.BaseController;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;






import javax.annotation.Resource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章分类列表管理控制层
 *
 * @author tanghom
 */
@Controller
@RequestMapping("/article_category")
public class ArticleCategoryController extends BaseController<ArticleCategory> {

    @Resource
    private ArticleCategoryService articleCategoryService;
    @Resource
    private ArticleService articleService;

    /**
     * 列表
     *
     * @param name
     * @param status
     * @param model
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST, RequestMethod.GET})
    public String list(String name, Integer status, Model model) {
        List<ArticleCategory> articleCategoryList = articleCategoryService.findListByParams(name, status);
        model.addAttribute("articleCategoryList", articleCategoryList);
        return THEME_PATH + "/article_category/list";
    }

    /**
     * 新增文章分类页面
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Long parentId,Model model) {
    	if(parentId!=null){
    		List<ArticleCategory> articleCategoryList = articleCategoryService.findListByParentId(parentId, null);
    		model.addAttribute("articleCategoryList", articleCategoryList);
    	}
        return THEME_PATH + "/article_category/edit";
    }

    /**
     * 编辑文章分类页面
     *
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, Long parentId,Model model) {
    	if(parentId!=null){
    		List<ArticleCategory> articleCategoryList = articleCategoryService.findListByParentId(parentId, null);
    		model.addAttribute("articleCategoryList", articleCategoryList);
    	}
        ArticleCategory articleCategory = articleCategoryService.findById(id);
        model.addAttribute("articleCategory", articleCategory);
        return THEME_PATH + "/article_category/edit";
    }

    /**
     * 保存文章分类
     *
     * @param articleCategory 文章分类对象
     * @return
     */
    @RequestMapping(value = "/save", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public String add(ArticleCategory articleCategory) {
        try {
            articleCategoryService.saveOrUpdate(articleCategory);
            return CommonUtils.msgCallback(true, "操作成功", "", null);
        } catch (Exception e) {
            return CommonUtils.msgCallback(false, "操作失败，请重试！", "", null);
        }
    }

    /**
     * 删除文章分类
     *
     * @param id 文章分类ID
     * @return 结果
     */
    @ResponseBody
    @RequestMapping(value = "/delete", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    public String delete(Long id) {
        try {
            List<Article> articles = articleService.findListByParams(id,null);
            if(null!=articles){
                return CommonUtils.msgCallback(false, "分类下有文章，不能删除", "", null);
            }
            articleCategoryService.delete(id);
            return CommonUtils.msgCallback(true, "删除文章分类成功", "", null);
        } catch (Exception ex) {
            return CommonUtils.msgCallback(false, "删除文章分类失败", "", null);

        }
    }
    

	public ArticleModule plugin = new ArticleModule() {

		public boolean viewModel(Article article, BaseController c) {
			if(article.getCategoryId()==null) return true;
			ArticleCategory articleCategory = articleCategoryService.findById(article.getCategoryId());
			c.setAttr("pathList", "category/"+articleCategory.getParentId()+"/"+article.getCategoryId());			
			
			List<ArticleCategory> articles  = articleCategoryService.findListByParams(article.getCategoryName(), 1);
			c.setAttr("childArticleCategoryList", articles);
			return true;
		}

		public boolean listModel(List<Article> articles, BaseController c) {
			c.setAttr("rootArticleCategoryList", articleCategoryService.getRootArticleCategoryList());
			
			List<ArticleCategory> allArticleCategory = articleCategoryService.findListByParams(null, 1);
			c.setAttr("allArticleCategoryList", allArticleCategory);
			Map<Long, List<ArticleCategory>> articleCategoryMap = new HashMap<Long, List<ArticleCategory>>();
			Map<Long, List<Article>> recommendArticleMap = new HashMap<Long, List<Article>>();
			Map<Long, List<Article>> hotArticleMap = new HashMap<Long, List<Article>>();
			Map<Long, List<Article>> newArticleMap = new HashMap<Long, List<Article>>();
			for (ArticleCategory articleCategory : allArticleCategory) {
				articleCategoryMap.put(articleCategory.getId(), articleCategoryService.findListByParentId(articleCategory.getId(),1));
				recommendArticleMap.put(articleCategory.getId(), articleService.getRecommendArticleList(articleCategory.getId(), Article.MAX_ARTICLE_LIST_COUNT));
				hotArticleMap.put(articleCategory.getId(), articleService.getHotArticleList(articleCategory.getId(), Article.MAX_ARTICLE_LIST_COUNT));
				newArticleMap.put(articleCategory.getId(), articleService.getNewArticleList(articleCategory.getId(), Article.MAX_ARTICLE_LIST_COUNT));
			}
			c.setAttr("articleCategoryMap", articleCategoryMap);
			c.setAttr("recommendArticleMap", recommendArticleMap);
			c.setAttr("hotArticleMap", hotArticleMap);
			c.setAttr("newArticleMap", newArticleMap);
			return true;
		}
	};
}
